package com.inspsys.inspection.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.inspsys.common.annotation.Log;
import com.inspsys.common.core.controller.BaseController;
import com.inspsys.common.core.domain.AjaxResult;
import com.inspsys.common.enums.BusinessType;
import com.inspsys.inspection.domain.InspectionPlan;
import com.inspsys.inspection.service.IInspectionPlanService;
import com.inspsys.common.utils.poi.ExcelUtil;
import com.inspsys.common.core.page.TableDataInfo;

/**
 * 巡检计划Controller
 *
 * @author inspsys
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/inspection/plan")
public class InspectionPlanController extends BaseController {
    @Autowired
    private IInspectionPlanService inspectionPlanService;

    /**
     * 查询巡检计划列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionPlan inspectionPlan) {
        startPage();
        List<InspectionPlan> list = inspectionPlanService.selectInspectionPlanList(inspectionPlan);
        return getDataTable(list);
    }

    /**
     * 导出巡检计划列表
     */
    @PreAuthorize("@ss.hasPermi('inspection:plan:export')")
    @Log(title = "巡检计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InspectionPlan inspectionPlan) {
        List<InspectionPlan> list = inspectionPlanService.selectInspectionPlanList(inspectionPlan);
        ExcelUtil<InspectionPlan> util = new ExcelUtil<InspectionPlan>(InspectionPlan.class);
        util.exportExcel(response, list, "巡检计划数据");
    }

    /**
     * 获取巡检计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('inspection:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(inspectionPlanService.selectInspectionPlanById(id));
    }

    /**
     * 新增巡检计划
     */
    @PreAuthorize("@ss.hasPermi('inspection:plan:add')")
    @Log(title = "巡检计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionPlan inspectionPlan) {
        return toAjax(inspectionPlanService.insertInspectionPlan(inspectionPlan));
    }

    /**
     * 修改巡检计划
     */
    @PreAuthorize("@ss.hasPermi('inspection:plan:edit')")
    @Log(title = "巡检计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionPlan inspectionPlan) {
        return toAjax(inspectionPlanService.updateInspectionPlan(inspectionPlan));
    }

    /**
     * 删除巡检计划
     */
    @PreAuthorize("@ss.hasPermi('inspection:plan:remove')")
    @Log(title = "巡检计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(inspectionPlanService.deleteInspectionPlanByIds(ids));
    }

    /**
     * 发布巡检计划
     */
    @PreAuthorize("@ss.hasPermi('inspection:plan:edit')")
    @Log(title = "巡检计划", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    public AjaxResult release(@RequestBody InspectionPlan inspectionPlan) {
        int result = inspectionPlanService.releaseInspectionPlanById(inspectionPlan.getId());
        if (result == 2) {
           return warn("该巡检计划已发布");
        }
        return toAjax(result);
    }
}
